package com.dg.admodule;

import net.rim.device.api.system.CoverageInfo;
import net.rim.device.api.system.RadioInfo;

import com.smaato.SmaatoAPI;

public class AdModule{
	private SmaatoAPI adApi;
	private boolean iRequestOutstanding;
	
	private static AdModule instance = null;
	
	private AdModule(){
		if (this.adApi == null){
			try{
				this.adApi = new SmaatoAPI(AdConstants.adSpaceId, AdConstants.pubId, AdConstants.adFormat, AdConstants.getGPS);
				init();
			} catch (Exception e){
				System.out.println("There was a problem setting a field - " + e.getMessage());
			}
		}
	}
	
	public static AdModule getInstance(){
		if (instance == null){
			instance = new AdModule();
		}
		
		return instance;
	}
	
	public SmaatoAPI getSmaatoApi() {
		if (this.adApi == null){
			try{
				this.adApi = new SmaatoAPI(AdConstants.adSpaceId, AdConstants.pubId, AdConstants.adFormat, AdConstants.getGPS);
				init();
			} catch (Exception e){
				System.out.println("There was a problem setting a field - " + e.getMessage());
			}
		}
		
		return this.adApi;
	}

	public boolean getIRequestOutstanding() {
		return iRequestOutstanding;
	}

	public void setIRequestOutstanding(boolean iRequestOutstanding) {
		this.iRequestOutstanding = iRequestOutstanding;
	}
	
	private void init(){
		if (this.adApi != null){
			try{
				this.adApi = new SmaatoAPI(AdConstants.adSpaceId, AdConstants.pubId, AdConstants.adFormat, AdConstants.getGPS);
				if (getConnectionString() != null){
					adApi.setConnectionParameter(getConnectionString());
				}
				if (AdConstants.height > 0){
					adApi.setHeight(AdConstants.height);
				}
				if (AdConstants.width > 0){
					adApi.setWidth(AdConstants.width);
				}
				if (AdConstants.textColor.length() > 0){
					adApi.setTextAdFgColor(Integer.parseInt(AdConstants.textColor, 16));
				}
				if (AdConstants.bgColor.length() > 0){
					adApi.setTextAdBgColor(Integer.parseInt(AdConstants.bgColor, 16));
				}
				if (AdConstants.age.length() > 0){
					adApi.setAge(Integer.parseInt(AdConstants.age));
				}
				if (AdConstants.dob.length() > 0){
					adApi.setDateOfBirth(AdConstants.dob);
				}
				if (AdConstants.gender.length() > 0){
					adApi.setGender(AdConstants.gender);
				}
				if (AdConstants.etnicity.length() > 0){
					adApi.setEthnicity(AdConstants.etnicity);
				}
				if (AdConstants.income.length() > 0){
					adApi.setIncome(AdConstants.income);
				}
				if (AdConstants.education.length() > 0){
					adApi.setEducation(AdConstants.education);
				}
				if (AdConstants.genderSought.length() > 0){
					adApi.setGenderSought(AdConstants.genderSought);
				}
				if (AdConstants.maritalStatus.length() > 0){
					adApi.setMaritalStatus(AdConstants.maritalStatus);
				}
				if (AdConstants.city.length() > 0){
					adApi.setCity(AdConstants.city);
				}
				if (AdConstants.country.length() > 0){
					adApi.setCountry(AdConstants.country);
				}
				if (AdConstants.state.length() > 0){
					adApi.setState(AdConstants.state);
				}
				if (AdConstants.zip.length() > 0){
					adApi.setZIP(AdConstants.zip);
				}
				if (AdConstants.latitude.length() > 0 && AdConstants.longtitude.length() > 0){
					adApi.setGPSCoords(Double.parseDouble(AdConstants.latitude), Double.parseDouble(AdConstants.longtitude));
				}
				if (AdConstants.keywordString.length() > 0){
					adApi.setKeywordString(AdConstants.keywordString);
				}
				if (AdConstants.queryString.length() > 0){
					adApi.setQueryString(AdConstants.queryString);
				}
			} catch (Exception e){
				System.out.println("There was a problem setting a field - " + e.getMessage());
			}
		}
	}
	
	private String getConnectionString()
	{
		String suffix = null;
        if (CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_MDS)) 
        {
              // BES is available
              suffix = ";deviceside=false";
        }
        else if (CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_BIS_B)) 
        {
              // BIS is available
              suffix = ";deviceside=false;ConnectionType=mds-public";
        }
        else if (RadioInfo.areWAFsSupported(RadioInfo.WAF_WLAN) && 
        		CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_DIRECT, RadioInfo.WAF_WLAN, false)) 
        {
              // wifi is available
              suffix = ";deviceside=true;interface=wifi";
        }
        else if (CoverageInfo.isCoverageSufficient(CoverageInfo.COVERAGE_DIRECT)) {
              // attempt to use direct tcp connection
              suffix = ";deviceside=true";
        }

        return suffix;
	}
}